<template>
  <div>
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="12">
              <a-form-item label="模板名称">
                <j-input placeholder="输入名称模板名称查询" v-model="queryParam.templateName"></j-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="模板类型">
                <a-input placeholder="请输入模板类型" v-model="queryParam.templateType"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQueryInfo" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- 操作按钮区域 -->
      <div class="table-operator" style="border-top: 5px">
        <a-button @click="handleAddInfO" type="primary" icon="plus" >添加模板</a-button>
      </div>

      <!-- table区域-begin -->
      <div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        </div>

        <a-table
          ref="table"
          bordered
          size="middle"
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          @change="handleTableChange">
          <span slot="action" slot-scope="text, record">
          <a @click="handleEditTemplate(record)" >编辑</a>
          <a-divider type="vertical" />
            <a @click="templateDelete(record)" >删除</a>
        </span>
        </a-table>
      </div>
    </a-card>
  </div>
</template>

<script>
import {putAction,getFileAccessHttpUrl} from '@/api/manage';
import {frozenBatch} from '@/api/api'
import {  templateList,templateAdd,templateEdit,templateDelete,templateQueryByid } from '@/api/temple'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'

export default {
  name: "UserList",
  mixins: [JeecgListMixin],
  components: {

  },
  data() {
    return {
      visible:true,
      description: '这是用户管理页面',
      queryParam: {},
      recycleBinVisible: false,
      columns: [
        {
          title: '模板名称',
          align: "center",
          dataIndex: 'templateName',
          width: 120,
          sorter: true
        },
        {
          title: '模板类型',
          align: "center",
          width: 120,
          dataIndex: 'templateType'
        },
        {
          title: '模板状态',
          align: "center",
          width: 80,
          dataIndex: 'templateStatus',
          sorter: true
        },
        {
          title: '模板地址',
          align: "center",
          width: 100,
          dataIndex: 'templatePath'
        },
        {
          title: '模板备注',
          align: "center",
          width: 100,
          dataIndex: 'templateMemo'
        },
        {
          title: '创建人',
          align: "center",
          width: 180,
          dataIndex: 'createBy'
        },
        {
          title: '创建时间',
          align: "center",
          width: 180,
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: {customRender: 'action'},
          align: "center",
          width: 170
        }

      ],
      url: {
        list: "/sys/website/sysTemplateManager/list",
        delete: "/sys/website/sysTemplateManager/delete",
        deleteBatch: "/sys/website/sysTemplateManager/deleteBatch",
        exportXlsUrl: "/sys/website/sysTemplateManager/exportXls",
      },
    }
  },
  computed: {},
  methods: {
    searchQueryInfo(){
      bannerListtemplateList(this.queryParam).then((res)=>{
        if(res.success){
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
      })
    },
    handleEditTemplate: function(record){
      console.log(JSON.stringify(record));
      this.$router.push({path:'/website/template/add',query:{id:record.id}});
    },

    templateDelete:  function(record){
      let  that  = this;
      if(record.id) {
        let demo  ={ id:record.id }
        templateDelete(demo).then(res=>{
          if(res.code == 200){
            that.$message.success('删除成功');
          }
        })
        that.loadData();
      }
    },
    handleAddInfO: function () {
      this.$router.push({path:'/website/template/add'});
    },
    getAvatarView: function (avatar) {
      return getFileAccessHttpUrl(avatar)
    },
    batchFrozen: function (status) {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return false;
      } else {
        let ids = "";
        let that = this;
        let isAdmin = false;
        that.selectionRows.forEach(function (row) {
          if (row.username == 'admin') {
            isAdmin = true;
          }
        });
        if (isAdmin) {
          that.$message.warning('管理员账号不允许此操作,请重新选择！');
          return;
        }
        that.selectedRowKeys.forEach(function (val) {
          ids += val + ",";
        });
        that.$confirm({
          title: "确认操作",
          content: "是否" + (status == 1 ? "解冻" : "冻结") + "选中账号?",
          onOk: function () {
            frozenBatch({ids: ids, status: status}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              } else {
                that.$message.warning(res.message);
              }
            });
          }
        });
      }
    },
    handleMenuClick(e) {
      if (e.key == 1) {
        this.batchDel();
      } else if (e.key == 2) {
        this.batchFrozen(2);
      } else if (e.key == 3) {
        this.batchFrozen(1);
      }
    },
    handleFrozen: function (id, status, username) {
      let that = this;
      //TODO 后台校验管理员角色
      if ('admin' == username) {
        that.$message.warning('管理员账号不允许此操作！');
        return;
      }
      frozenBatch({ids: id, status: status}).then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          that.loadData();
        } else {
          that.$message.warning(res.message);
        }
      });
    },

  }

}
</script>
<style scoped>
@import '~@assets/less/common.less'
</style>